package com.talent.jiwen.my;

import android.support.annotation.NonNull;
import android.support.v4.content.ContextCompat;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;

import com.scwang.smartrefresh.layout.SmartRefreshLayout;
import com.scwang.smartrefresh.layout.api.RefreshLayout;
import com.scwang.smartrefresh.layout.listener.OnLoadMoreListener;
import com.scwang.smartrefresh.layout.listener.OnRefreshListener;
import com.talent.jiwen.R;
import com.talent.jiwen.base.BaseActivity;
import com.talent.jiwen.http.common.ActivityLifeCycleEvent;
import com.talent.jiwen.http.common.ApiFactory;
import com.talent.jiwen.http.common.HttpUtil;
import com.talent.jiwen.http.common.ProgressSubscriber;
import com.talent.jiwen.http.result.CollectTeacherListResult;
import com.talent.jiwen.teacher.TeacherDetailActivity;
import com.talent.jiwen.util.ImageUtil;
import com.talent.jiwen.util.SPConstant;
import com.talent.jiwen.util.SPUtil;
import com.talent.jiwen.util.Validators;
import com.zhy.adapter.recyclerview.CommonAdapter;
import com.zhy.adapter.recyclerview.base.ViewHolder;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import butterknife.BindView;
import rx.Observable;

public class CollectListActivity extends BaseActivity {
    @BindView(R.id.numTv)
    TextView numTv;
    @BindView(R.id.refreshLayout)
    SmartRefreshLayout refreshLayout;
    @BindView(R.id.noDataIv)
    ImageView noDataIv;
    @BindView(R.id.recyclerView)
    RecyclerView recyclerView;

    private List<CollectTeacherListResult.CollectTeacher> dataList = new ArrayList<>();

    @Override
    protected int provideContentViewId() {
        return R.layout.activity_collect_list;
    }

    @Override
    public String titleName() {
        return getString(R.string.my_collect);
    }

    @Override
    protected void initData() {

        LinearLayoutManager layoutManager = new LinearLayoutManager(this);
        recyclerView.setLayoutManager(layoutManager);

        recyclerView.setAdapter(new CommonAdapter<CollectTeacherListResult.CollectTeacher>(CollectListActivity.this , R.layout.item_collect , dataList){
            @Override
            protected void convert(ViewHolder holder, final CollectTeacherListResult.CollectTeacher data, int position) {
                ImageView headIv = (ImageView)holder.getView(R.id.headIv);
                TextView nameTv = (TextView)holder.getView(R.id.nameTv);
                TextView desTv = (TextView)holder.getView(R.id.desTv);
                TextView statusTv = (TextView)holder.getView(R.id.statusTv);
                View divider = (View)holder.getView(R.id.divider);

                ImageUtil.loadCorner4Image(CollectListActivity.this , data.getHeadImage() , headIv);
                if (!Validators.isEmpty(data.getTeacherName())){
                    nameTv.setText(data.getTeacherName());
                }
                float score = (Float.parseFloat(data.getEffectScore()) +Float.parseFloat(data.getServeScore())) / 2;
                desTv.setText("评分 " + score + "  解决次数 " + data.getSolveCount());

                if (data.getBusyStatus() == 0){
                    //可接单
                    statusTv.setBackgroundResource(R.drawable.bg_circle_theme);
                    statusTv.setTextColor(ContextCompat.getColor(CollectListActivity.this , R.color.white));
                    statusTv.setText("可接单");
                }else{
                    //忙碌中
                    statusTv.setBackgroundResource(R.drawable.bg_circle_gray);
                    statusTv.setTextColor(ContextCompat.getColor(CollectListActivity.this , R.color.text_gray_9));
                    statusTv.setText("忙碌中");
                }

                if (position == dataList.size() - 1){
                    divider.setVisibility(View.GONE);
                }else{
                    divider.setVisibility(View.VISIBLE);
                }

                holder.itemView.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View view) {
                        TeacherDetailActivity.startTeacherDetailActivity(CollectListActivity.this , TeacherDetailActivity.TYPE_ASK_PROBLEM  , data.getTeacherId() , "");
                    }
                });
            }
        });

        refreshLayout.setOnRefreshListener(new OnRefreshListener() {
            @Override
            public void onRefresh(@NonNull RefreshLayout refreshLayout) {
                getCollectedTeacherList();
            }
        });

        refreshLayout.setOnLoadMoreListener(new OnLoadMoreListener() {
            @Override
            public void onLoadMore(@NonNull RefreshLayout refreshLayout) {
                refreshLayout.finishLoadMoreWithNoMoreData();
            }
        });

        refreshLayout.autoRefresh();
    }

    /**获取收藏教师列表
     *
     */
    private void getCollectedTeacherList(){
        Map<String , String> map = new HashMap<>();
        map.put("studentId" , SPUtil.getUserId());

        Observable observable = ApiFactory.getEducationApiSingleton().getCollectedTeacherList(SPUtil.stringOut(SPConstant.TOKEN) , map);
        HttpUtil.getInstance().toSubscribe(observable, new ProgressSubscriber<CollectTeacherListResult>(this) {
            @Override
            protected void _onNext(CollectTeacherListResult collectTeacherListResult) {
                dataList.clear();

                for (CollectTeacherListResult.CollectTeacher collectTeacher : collectTeacherListResult.getTeacherList()){
                    if (null != collectTeacher.getTeacherId()){
                        dataList.add(collectTeacher);
                    }
                }

                recyclerView.getAdapter().notifyDataSetChanged();
                numTv.setText(dataList.size() + "");

                if (Validators.isEmpty(dataList)){
                    noDataIv.setVisibility(View.VISIBLE);
                }else{
                    noDataIv.setVisibility(View.GONE);
                }

                refreshLayout.finishRefresh();
            }

            @Override
            protected void _onError(String message) {
                showToast(message);

                noDataIv.setVisibility(View.VISIBLE);

                refreshLayout.finishRefresh();
            }
        }, ActivityLifeCycleEvent.DESTROY, lifecycleSubject,false);
    }

}
